package org.graduates.webapp.actions.file;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.graduates.webapp.actions.BaseAction;
import org.projects.graduates.Constants;
import org.projects.graduates.utils.FileCommon;

import com.opensymphony.xwork2.ActionSupport;

@Results(value = { @Result(name = "success", type = "json") })
public class FileUploadAction extends BaseAction {
	private static final long serialVersionUID = -2505044109663833035L;

	private File imgFile;

	// 上传到的文件夹:{image,flash,media,file}
	private String dir;
	/**
	 * 文件名称
	 */
	private String imgFileFileName;

	// kindeditor在线编辑器必需
	private int error = 0;

	// kindeditor在线编辑器必需
	private String message;

	// 上传成功后，编辑器引用图片的地址
	private String url;

	public String execute() {
		
		if (StringUtils.isEmpty(imgFileFileName) || null == imgFile) {
			setError("请选择文件");
			return ActionSupport.SUCCESS;
		}

		if (noAllowType(dir, FileCommon.extOf(imgFileFileName))) {
			setError("上传文件扩展名是不允许的扩展名。\n只允许" + createExtMap().get(dir) + "格式。");
			return ActionSupport.SUCCESS;
		}

		if (imgFile.length() > Constants.MAX_FILE_SIZE) {
			setError("文件最大为" + Constants.MAX_FILE_SIZE / (1024 * 1024) + " b");
			return ActionSupport.SUCCESS;
		}
		
		
		FileCommon.write(imgFile, imgFileFileName, whereToSave());
		
		
		url = fileUrl();

		return ActionSupport.SUCCESS;
	}

	private boolean noAllowType(String dir, String fileExt) {
		if (Arrays.<String> asList(createExtMap().get(dir).split(",")).contains(fileExt)) {
			return false;
		}
		return true;
	}

	private void setError(String message) {
		this.error = 1;
		this.message = message;
	}

	private HashMap<String, String> createExtMap() {
		HashMap<String, String> extMap = new HashMap<String, String>();
		extMap.put("image", "gif,jpg,jpeg,png,bmp");
		extMap.put("flash", "swf,flv");
		extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
		extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
		return extMap;
	}
	
	private String whereToSave(){
		return ServletActionContext.getServletContext().getRealPath(Constants.UPLOAD_DIR) + File.separator + getCurrentPersonId() + File.separator + dir + File.separator ;
	}

	private String fileUrl(){
		String result = "";
		result = ServletActionContext.getRequest().getContextPath() + "/" + Constants.UPLOAD_DIR;
		return result += "/" + getCurrentPersonId() + "/" + dir + "/" + imgFileFileName ;
	}
	
	public int getError() {
		return error;
	}

	public void setError(int error) {
		this.error = error;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
